home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_2 / a2_3.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  2.2 KB  |  92 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 2.3 (False position or Regula Falsi Method).
  9. % Section    2.2,    Bracketing Methods for Locating a Root, Page 62
  10. echo on; clc; format long; hold off; clear
  11. % This program implements the Regula Falsi method.
  12.  
  13. %    Define and store the function f(x) in the M-file f.m
  14. % function y = f(x)
  15. % y = x.*sin(x) - 1;
  16.  
  17. delete f.m
  18. diary f.m; disp('function y = f(x)');...
  19.            disp('y = x.*sin(x) - 1;');...
  20. diary off;
  21.  
  22. % Remark. f.m and regula.m are used for Algorithm 2.3  
  23. f(0); % Test for file f.m
  24. pause    % Press any key to see the graph y = f(x).
  25.  
  26. clc; clg;
  27. a = 0;
  28. b = 2;
  29. c = -1;
  30. d = 1;
  31. h = (b-a)/150;
  32. X = a:h:b;
  33. Y = f(X);
  34. axis([a b c d]);...
  35. plot(X,Y,'-g');...
  36. hold on;...
  37. plot([a b],[0 0],'b',[0 0],[c d],'b');...
  38. xlabel('x');...
  39. ylabel('y');...
  40. title('Graph of y = f(x).');...
  41. grid;...
  42. axis;...
  43. hold off;...
  44. shg; pause    % Press any key to continue.
  45.  
  46. clc;
  47. %    Place the starting endpoints for [a,b] in a  and  b
  48.  
  49. %    Place the abscissa tolerance in  delta
  50.  
  51. %    Place the ordinate tolerance in  epsilon
  52.  
  53. %    Place the maximum number of iterations in  max1
  54.  
  55. a = 0;  
  56. b = 2; 
  57. delta = 1e-6;
  58. epsilon = 1e-6;
  59. max1 = 50;
  60.  
  61. [p,yp,err,P] = regula('f',a,b,delta,epsilon,max1);
  62.  
  63. pause    % Press any key for the list of iterations.
  64.  
  65. clc; clg;
  66. [m1 m2] = size(P);
  67. n0 = min(7,m1);
  68. Xa = P(1:n0,1); Xa = [a,Xa',b];
  69. Xb = P(1:n0,2); Xb = [a,Xb',b];
  70. Z0 = zeros(1,n0+2);
  71. axis([0 2 -1 1]);...
  72. plot(X,Y,'-g',Xa,Z0,'or',Xb,Z0,'or');...
  73. hold on;...
  74. plot([a b],[0 0],'b',[0 0],[c d],'b');...
  75. xlabel('x');...
  76. ylabel('y');...
  77. title('Graphical analysis for the Regula Falsi method.');...
  78. grid;...
  79. axis;...
  80. hold off;...
  81. shg; pause    % Press any key to continue.
  82.  
  83. Mx1 = 'Iterations for the Regula-Falsi method.';
  84. Mx2 = '     a                  b';
  85. Mx3 = 'The approximate root is:';
  86. Mx4 = 'The error estimate for p is  ± ';
  87. clc,echo off, diary output,...
  88. disp(''),disp(Mx1),disp(''),disp(Mx2),disp(P),...
  89. disp(''),disp(Mx3),disp(''),disp('p = '),disp(p),...
  90. disp('f(p) = '),disp(yp),disp(''),...
  91. disp([Mx4,num2str(err)]),diary off, echo on
  92.